package com.study.designmodes.service.component;

import org.springframework.util.StringUtils;

/**
 * 数据屏蔽函数
 *
 * @author yangyanbin
 * @date 2023/02/03
 * @since 20230203
 */
public enum DataMaskingFunc {
    /**
     * 脱敏转换器
     */
    NO_MASK((str, maskChar, index, length) -> {
        return str;
    }),
    ALL_MASK((str, maskChar, index, length) -> {
        if (StringUtils.hasLength(str)) {
            char[] chars = str.toCharArray();
            int endIndex = Math.min(index + length, str.length());
            for (int i = index; i < endIndex; i++) {
                chars[i] = DataMaskingOperation.MASK_CHAR.charAt(0);

            }
            return String.valueOf(chars);

/*            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 4; i++) {
                sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
            }
            return sb.toString();*/
        } else {
            return str;
        }
    });

    private final DataMaskingOperation operation;

    private DataMaskingFunc(DataMaskingOperation operation) {
        this.operation = operation;
    }

    public DataMaskingOperation operation() {
        return this.operation;
    }
}
